home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / demo / rotplnnr.lha / wbargs.c < prev    next >
C/C++ Source or Header  |  1994-08-21  |  4KB  |  136 lines

  1. /* Copyright (c) 1993 SAS Institute, Inc, Cary, NC USA */
  2. /* All Rights Reserved */
  3.  
  4. #include <workbench/startup.h>
  5. #include <exec/execbase.h>
  6. #include <string.h>
  7. #include <dos.h>
  8. #include <stdlib.h>
  9.  
  10. #include <proto/dos.h>
  11. #include <proto/exec.h>
  12. #include <proto/icon.h>
  13.  
  14. /* These two symbols, _WBArgc and _WBArgv, are initialized if     */
  15. /* the program was invoked from WorkBench.  They look like normal */
  16. /* C (argc, argv) parameters.  The parameters are gathered as     */
  17. /* follows:                                                       */
  18. /*   Name of the program                                          */
  19. /*   Any tooltypes specified in the ToolTypes array               */
  20. /*   Any icons supplied as arguments (with SHIFT-CLICK)           */
  21.  
  22. int _WBArgc;       /* Count of the number of WorkBench arguments */
  23. char **_WBArgv;    /* The actual arguments                       */
  24.  
  25. static int _WBArgMax;  /* Internal: tells us how much space is left */
  26.                        /* in _WBArgv.                               */
  27.  
  28. #define V37 (SysBase->LibNode.lib_Version >= 36) 
  29.  
  30. // NAME:    FullName
  31. // PURPOSE: get the full pathname of a file.
  32. // NOTES:   Under AmigaDOS 2.0, use NameFromLock.
  33. //          Under 1.3, we should do this ourselves, but we just copy
  34. //          the incoming name over.
  35. static void FullName(BPTR parent, char *name, char *buf, int len)
  36. {
  37.    int i = 0;
  38.    struct ExecBase *SysBase = *(struct ExecBase **)4;
  39.  
  40.    if(V37)
  41.    {
  42.       if(NameFromLock(parent, buf, len-1))
  43.       {
  44.          i = strlen(buf);
  45.          if(buf[i-1] != ':' && buf[i-1] != '/')
  46.          {
  47.             buf[i++] = '/';
  48.             buf[i] = 0;
  49.          }
  50.       }
  51.    }
  52.  
  53.    if(i < len-1) strncpy(buf+i, name, len-i-1);
  54.  
  55.    return;
  56. }
  57.  
  58. /* Add an argument to the _WBArgv array.  We must allocate */
  59. /* memory for the argument and copy the incoming data.     */
  60. static int AddArg(char *arg)
  61. {
  62.    if(_WBArgc >= _WBArgMax-1)
  63.    {
  64.       /* Out of space in _WBArgv.  Reallocate it bigger. */
  65.       _WBArgMax += 10;
  66.       _WBArgv = realloc(_WBArgv, _WBArgMax*sizeof(char *));
  67.       if(_WBArgv == NULL) return -1;
  68.    }
  69.  
  70.    /* Allocate memory for the new argument */
  71.    _WBArgv[_WBArgc] = malloc(strlen(arg)+1);
  72.    if(_WBArgv[_WBArgc] == NULL) return -1;
  73.  
  74.    /* Copy the argument data over */
  75.    strcpy(_WBArgv[_WBArgc], arg);
  76.  
  77.    /* Increment our argument count */
  78.    _WBArgc++;
  79.  
  80.    return 0;
  81. }
  82.  
  83. /* This is an autoinitializer routine that will be run if this  */
  84. /* module is linked in to user code.  Referring to the external */
  85. /* data items above is sufficient to pull this in from a library*/
  86.  
  87. int __stdargs _STI_20000_WBArgParse(void)
  88. {
  89.    struct WBArg *wba;
  90.    int nargs;
  91.    char buf[512];
  92.  
  93.    if(_WBenchMsg == NULL) return 0;  // Not invoked from WorkBench
  94.  
  95.    /* Put the program name in */
  96.    wba=_WBenchMsg->sm_ArgList;
  97.    FullName(wba->wa_Lock, wba->wa_Name, buf, sizeof(buf));
  98.    if(AddArg(buf)) return -1;
  99.    
  100.    /* HACKED OUT -- chris
  101.  
  102.    /* Find the tool types and add them */
  103.  
  104.    if(IconBase = OpenLibrary("icon.library", 0L))
  105.    {
  106.       dir = CurrentDir(wba->wa_Lock);
  107.       if(dob = GetDiskObject(wba->wa_Name))
  108.       {
  109.          if(dob->do_ToolTypes)
  110.          {
  111.             for(nargs=0; dob->do_ToolTypes[nargs]; nargs++)
  112.                if(AddArg(dob->do_ToolTypes[nargs])) return -1;
  113.          }
  114.          FreeDiskObject(dob);
  115.       }
  116.       CurrentDir(dir);
  117.       CloseLibrary(IconBase);
  118.    }
  119.    */
  120.    
  121.    /* Now add the file arguments */
  122.    for(nargs=1, wba++; 
  123.        nargs<_WBenchMsg->sm_NumArgs; 
  124.        nargs++, wba++)
  125.    {
  126.       FullName(wba->wa_Lock, wba->wa_Name, buf, sizeof(buf));
  127.       if(AddArg(buf)) return -1;
  128.    }
  129.    
  130.    /* Make sure _WBArgv is terminated with a NULL pointer */
  131.    /* like ANSI C argv lists are.                         */
  132.    _WBArgv[_WBArgc] = NULL;
  133.  
  134.    return 0;
  135. }
  136.